﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Interfleet.SE.UICompiler.Core.ClosureCompilerREST
{
    /// <summary>
    /// This helper class executes Http posts to the Closure Compiler REST API and
    /// returns the response as a <see cref="System.String"/>
    /// </summary>
    internal static class CCRESTCaller
    {
        /// <summary>
        /// Posts the supplied data to the REST API
        /// </summary>
        /// <param name="postData">the data to post</param>
        /// <returns>the response from the REST API</returns>
        internal static string Call(string postData)
        {
            var request = HttpWebRequest.Create("http://closure-compiler.appspot.com/compile");
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            using (var writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postData);
            }
            var response = request.GetResponse();
            string responseAsString = string.Empty;
            using (var reader = new StreamReader(response.GetResponseStream()))
            {

                responseAsString = reader.ReadToEnd();
            }
            return responseAsString;
        }
    }
}
